{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "def write2json(path, data):\n",
    "    with open(path, 'w') as f:\n",
    "        f.write(json.dumps(data, ensure_ascii=False))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "tag2id = {}\n",
    "id2tag = {}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "type2id = json.loads(open('type2id.json', 'r').readline())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "tag2id['O'] = 0\n",
    "id2tag[0] = 'O'\n",
    "for type_ in type2id.keys():\n",
    "    if type_ != 'O':\n",
    "        B_tag = 'B-'+type_\n",
    "        I_tag = 'I-'+type_\n",
    "        tag2id[B_tag] = 2*(type2id[type_]-1) + 1\n",
    "        tag2id[I_tag] = 2*(type2id[type_]-1) + 2\n",
    "        id2tag[2*(type2id[type_]-1) + 1] = B_tag\n",
    "        id2tag[2*(type2id[type_]-1) + 2] = I_tag"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "write2json('tag2id.json', tag2id)\n",
    "write2json('id2tag.json', id2tag)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
